package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.BrandDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandDao brandDao;

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    CategoryBrandRelationDao categoryBrandRelationDao;

    @Autowired
    BrandService brandService;

    @Override
    public List<BrandEntity> getRelationBrandsList(long catID) {
        //根据分类id查询品牌分类关联表，得到所有符合条件的数据
        List<CategoryBrandRelationEntity> categoryBrandsList = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catID));
        //根据品牌分类关联表内的品牌id去品牌表中查询所有符合条件的数据
        List<BrandEntity> collect = categoryBrandsList.stream().map(item -> {
            BrandEntity byId = brandService.getById(item.getBrandId());
            return byId;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 修改分类时同步修改品牌与分类关联表内数据
     * @param name  需要修改的分类名称
     * @param catId 需要修改的分类id
     */
    @Override
    public void updateCascade(String name, Long catId) {
        this.baseMapper.updateCascade(name,catId);
    }

    /**
     * 新增关联分类
     * @param categoryBrandRelation
     * @return
     */
    @Override
    public boolean saveDetails(CategoryBrandRelationEntity categoryBrandRelation) {
        //根据品牌id查询品牌信息
        BrandEntity brandEntity = brandDao.selectById(categoryBrandRelation.getBrandId());
        //根据分类id查询分类信息
        CategoryEntity categoryEntity = categoryDao.selectById(categoryBrandRelation.getCatelogId());
        //将品牌名称set到品牌与分类关联表
        categoryBrandRelation.setBrandName(brandEntity.getName());
        //将分类名称set到品牌与分类关联表
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        boolean save = this.save(categoryBrandRelation);
        return save;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

}